<?php echo $this->Html->addCrumb('Quản lý sản phẩm')?>
<div class="accountPage">
>> <?php echo $this->Html->link('Thêm sản phẩm',array('action'=>'add','controller'=>'products'))?>

<legend>Tìm kiếm</legend>
<?php echo $this->Form->create('Product',array('type'=>'get','id'=>'searchForm'))?>
<table width='100%'>
<tr>
	<td><?php echo $this->Form->input('name',array('required'=>false,'label'=>'Tên sản phẩm','type'=>'text','value'=>$name))?></td>
	<td><?php echo $this->Form->input('product_code',array('required'=>false,'label'=>'Mã sản phẩm','type'=>'text','value'=>$product_code,'size'=>10))?></td>
	<td><?php echo $this->Form->input('category_id',array('empty'=>true,'required'=>false,'label'=>'Loại sản phẩm','options'=>$opt_cat,'selected'=>$category_id))?></td>
	<td><?php echo $this->Form->input('brand_id',array('empty'=>true,'required'=>false,'label'=>'Nhà sản xuất','options'=>$opt_brand,'selected'=>$brand_id))?></td>
</tr>
<tr>
	<td colspan='4'><?php echo $this->Form->submit('Bắt đầu tìm',array('class'=>'btn','div'=>false))?><?php echo $this->Form->reset('Xóa trắng',array('class'=>'btn','div'=>false))?></td>
</tr>
</table>
<?php echo $this->Form->end()?>

<div id='searchResult'>
<h3>Danh sách sản phẩm</h3>
<table id='tableCart'>
<col width='5%'/>
<col width='10%'/>
<col width='20%'/>
<col width='15%'/>
<col width='15%'/>
<col width='15%'/>
<col width='5%'/>
<col width='15%'/>
<tr>
	<th>STT</th>
	<th>Mã sản phẩm</th>
	<th>Tên sản phẩm</th>	
	<th>Loại sản phẩm</th>
	<th>Nhà sản xuất</th>
	<th>Hình ảnh</th>	
	<th></th>
	<th></th>	
</tr>
<?php
if($rs):
$i = 0;
foreach($rs as $item):
$i++;
?>
<tr>
	<td><?php echo $i?></td>
	<td><?php echo h($item['Product']['product_code'])?></td>
	<td><?php echo h($item['Product']['name'])?></td>
	<td><?php echo h($item['Category']['name'])?></td>
	<td><?php echo h($item['Brand']['name'])?></td>
	<td>
	<?php echo $this->Html->image('/../upload_files/'.$item['Product']['photo_dir'].'/small_'.$item['Product']['photo'])?>
	</td>	
	<td style='text-align:center'><?php echo $item['Product']['del_flag']?'X':'O'?></td>
	<td>
	<?php echo $this->Html->link('Sửa',array('action'=>'edit','controller'=>'products',h($item['Product']['id'])),array('class'=>'btn'))?>
	<?php echo $this->Html->link('Xóa',array('action'=>'delete','controller'=>'products',h($item['Product']['id'])),array('class'=>'btn'),'Bạn có chắc là muốn xóa?')?>
	</td>
</tr>
<?php endforeach;?>
<?php else:?>
<tr><td colspan='7'>Không tìm thấy dữ liệu</td></tr>
<?php endif;?>
</table>
<?php echo $this->element('paging')?>
</div>

</div>
<script language='javascript' type='text/javascript'>
$().ready(function(){
	$('#searchForm').submit(function(e){
		e.preventDefault();
		var name = $('input[name="name"]').val();
		var product_code = $('input[name="product_code"]').val();
		var category_id = $('select[name="category_id"]').val();		
		var brand_id = $('select[name="brand_id"]').val();
		$.ajax({
			url : '<?php echo $this->Html->url(array('action'=>'index','controller'=>'products'))?>',
			data : {
				name : name,
				product_code : product_code,
				brand_id : brand_id,
				category_id : category_id,				
			},
			//dataType : 'json',
			success : function(html, textStatus) {
				$('#searchResult').html(html);
				//alert('Success ' + textStatus + html);
			},
			error : function(xhr, textStatus, errorThrown) {
				//alert('An error occurred! ' + errorThrown);
			}
		});
	});
});
</script>